package com.mybatistest.controller;

import com.mybatistest.common.ResponseData;
import com.mybatistest.entity.Result;
import com.mybatistest.entity.User;
import com.mybatistest.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "MySQL测试")
@RestController
@RequestMapping("/mybatistest/user")
@Slf4j
@Validated
public class UserController {
    @Resource
    IUserService userService;

    @ApiOperation(value = "插入数据接口", notes = "测试MySQL插入数据功能")
    @PostMapping("/insert")
    public ResponseData<User> insert( @RequestBody @Validated User user)
    {
        int result = userService.insert(user);
        if (result > 0){
            return ResponseData.success("已成功插入" + result + "条数据");
        }
        return ResponseData.fail("插入失败");
    }

    @PutMapping("/update")
    public ResponseData<User> updateUser( @RequestBody User user) {
        boolean result = userService.updateUser(user);
        if (result){
            return ResponseData.success("已成功更新数据");
        }
        return ResponseData.fail("更新失败");
    }

    @GetMapping("/getByName")
    public ResponseData<List<Result>> queryByName(@RequestParam int age)
    {
        List<Result> results = userService.queryByName(age);
        return ResponseData.success(results);
    }

    @GetMapping("/total")
    public ResponseData<Integer> getTotal(@RequestParam @NotBlank String name) {
        Integer total = userService.totalCount(name);
        return ResponseData.success("符合条件共" + total + "条数据");
    }
}
